home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter2 / choosefo.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  225 lines

  1.  
  2. #include <windows.h>  
  3. #include "choosefo.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "ChooseFont()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105.  
  106. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  107. {
  108. static LOGFONT lf;
  109. static DWORD   dwColor = 0;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE :
  114.               memset( &lf, 0, sizeof( LOGFONT ) );
  115.               break;
  116.  
  117.       case WM_PAINT :
  118.               if ( lf.lfFaceName[0] )
  119.               {
  120.                  PAINTSTRUCT ps;
  121.                  HFONT       hFont, hOldFont;
  122.                  DWORD       dwOldColor;
  123.  
  124.                  // Create the font to use.
  125.                  //........................                                     
  126.                  hFont = CreateFontIndirect( &lf );
  127.  
  128.                  // Display sample text using the font.
  129.                  //....................................
  130.                  BeginPaint( hWnd, &ps );
  131.                  hOldFont   = SelectObject( ps.hdc, hFont );
  132.                  dwOldColor = SetTextColor( ps.hdc, dwColor );
  133.  
  134.                  TextOut( ps.hdc, 10, 10, "Sample Text", 11 );
  135.  
  136.                  SelectObject( ps.hdc, hOldFont );
  137.                  SetTextColor( ps.hdc, dwOldColor );
  138.                  EndPaint( hWnd, &ps );
  139.  
  140.                  // Delete the hFont.
  141.                  //..................
  142.                  DeleteObject( hFont );
  143.               }
  144.               else
  145.                  return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  146.  
  147.               break;
  148.  
  149.       case WM_COMMAND :
  150.               switch( LOWORD( wParam ) )
  151.               {
  152.                  case IDM_TEST :
  153.                         {
  154.                            CHOOSEFONT cf;
  155.  
  156.                            // Initialize the CHOOSEFONT structure.
  157.                            //......................................
  158.                            memset( &cf, 0, sizeof( CHOOSEFONT ) );
  159.                            cf.lStructSize  = sizeof( CHOOSEFONT );
  160.                            cf.hwndOwner    = hWnd;
  161.                            cf.lpLogFont    = &lf;
  162.                            cf.Flags        = CF_SCREENFONTS | CF_EFFECTS;
  163.                            cf.rgbColors    = dwColor;
  164.  
  165.                            if ( lf.lfFaceName[0] )
  166.                               cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  167.  
  168.                            // Create the choose font dialog.
  169.                            //...............................
  170.                            if ( ChooseFont( &cf ) )
  171.                            {
  172.                               // If the user chooses a font,
  173.                               // repaint the sample text.
  174.                               //............................
  175.                               dwColor = cf.rgbColors;
  176.                               InvalidateRect( hWnd, NULL, TRUE );
  177.                            }
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.  
  191.       case WM_DESTROY :
  192.               PostQuitMessage(0);
  193.               break;
  194.  
  195.       default :
  196.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  197.    }
  198.  
  199.    return( 0L );
  200. }
  201.  
  202.  
  203. LRESULT CALLBACK About( HWND hDlg,           
  204.                         UINT message,        
  205.                         WPARAM wParam,       
  206.                         LPARAM lParam)
  207. {
  208.    switch (message) 
  209.    {
  210.        case WM_INITDIALOG: 
  211.                return (TRUE);
  212.  
  213.        case WM_COMMAND:                              
  214.                if (   LOWORD(wParam) == IDOK         
  215.                    || LOWORD(wParam) == IDCANCEL)    
  216.                {
  217.                        EndDialog(hDlg, TRUE);        
  218.                        return (TRUE);
  219.                }
  220.                break;
  221.    }
  222.  
  223.    return (FALSE); 
  224. }
  225.